package com.xcl.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JwtUtil {
    private static final String KEY = "xyz";
    //生成Token令牌
    public static String genToken(Map<String,Object> claims){
         return JWT.create()
                .withClaim("claims",claims)
                .withExpiresAt(new Date(System.currentTimeMillis()+ 2*60*60*1000))
                .sign(Algorithm.HMAC256(KEY));
    }
    //验证Token令牌
    public static Map<String,Object> parseToken(String token){
            return JWT.require(Algorithm.HMAC256(KEY)).build().verify(token).getClaim("claims").asMap();
    }
    //解码Token令牌
    public static void main(String[] args) {
//        Map<String,Object> claims=new HashMap<>();
//        claims.put("userId",1);
//        claims.put("password","cnyh");
//        String token= genToken(claims);
//        System.out.println(token);
        String token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjbGFpbXMiOnsicGFzc3dvcmQiOiJjbnloIiwidXNlcklkIjoxfSwiZXhwIjoxNzQ0ODY2NzI3fQ.03BiWpAEC9V6ZOUSnKvmJ2XgxR3je75ShfvmCfSXGoE";
        Map<String,Object> map=parseToken(token);
        System.out.println(map);
    }
}
